from alibabacloud_tea_openapi.client import Client as OpenApiClient
from alibabacloud_credentials.client import Client as CredentialClient
from alibabacloud_tea_openapi import models as open_api_models
from alibabacloud_tea_util import models as util_models
from alibabacloud_bailian20231229.client import Client as bailian20231229Client
from app.database.db_setup import Base 
from sqlalchemy import Column, Integer, String, ForeignKey
from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base
class Sample:
    def __init__(self):
        pass

    @staticmethod
    def create_client(access_key_id, access_key_secret) -> bailian20231229Client:
        """
        使用 AK 初始化账号 Client
        @return: Client
        @throws Exception
        """
        config = open_api_models.Config(
            access_key_id=access_key_id,
            access_key_secret=access_key_secret
        )
        # Endpoint 请参考 https://api.aliyun.com/product/bailian
        config.endpoint = f'bailian.cn-beijing.aliyuncs.com'
        return bailian20231229Client(config)

    @staticmethod
    def create_api_info(
        workspace_id: str,
    ) -> open_api_models.Params:
        """
        API 相关
        @param path: string Path parameters
        @return: OpenApi.Params
        """
        params = open_api_models.Params(
            # 接口名称,
            action='AddCategory',
            # 接口版本,
            version='2023-12-29',
            # 接口协议,
            protocol='HTTPS',
            # 接口 HTTP 方法,
            method='POST',
            auth_type='AK',
            style='ROA',
            # 接口 PATH,
            pathname=f'/{workspace_id}/datacenter/category/',
            # 接口请求体内容格式,
            req_body_type='formData',
            # 接口响应体内容格式,
            body_type='json'
        )
        return params

    @staticmethod
    def call_add_category(
        access_key_id:str,
        access_key_secret:str,
        workspace_id: str,
        category_name: str ,
        category_type: str 
    ):
        client = Sample.create_client(access_key_id, access_key_secret)
        params = Sample.create_api_info(workspace_id)
        # body params
        body = {}
        body['CategoryName'] = category_name
        body['CategoryType'] = category_type
        # runtime options
        runtime = util_models.RuntimeOptions()
        request = open_api_models.OpenApiRequest(
            body=body
        )
        result = client.call_api(params, request, runtime)
        return result
class AlCategory(Base):
    __tablename__ = "al_category"
    id = Column(Integer, primary_key=True, autoincrement=True, comment='ID')
    script_id = Column(Integer, nullable=False, comment='话术id')
    name = Column(String(255), nullable=False, default='', comment='名称')
    CategoryId = Column(String(200), nullable=False, default='', comment='阿里云百炼CategoryId')
    add_time = Column(Integer, nullable=False, default=0, comment='添加时间')